package com.fa.dv.controller;

import cn.hutool.core.lang.tree.Tree;
import com.fa.common.core.domain.AjaxResult;
import com.fa.dv.domain.MachineryType;
import com.fa.dv.domain.vo.MachineryTypeVO;
import com.fa.dv.service.IMachineryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("dv/type")
public class MachineryTypeController {
    @Autowired
    private IMachineryTypeService machineryTypeService;
    /**
     * 查询所有设备类型
     */
    @GetMapping("/list")
    public AjaxResult treeList(){
        MachineryType machineryType = new MachineryType();
        List<Tree<String>> list =  machineryTypeService.tree();
        return AjaxResult.success(list);
    }

    @GetMapping("/getInfoType")
    public AjaxResult list(MachineryType machineryType){
        List<MachineryType> machineryTypes = machineryTypeService.selectMachineryTypeInfo(machineryType);
        //int parentIdByName = machineryTypeService.selectParentIdByName(machineryType.getMachineryTypeName());
        //List<Tree<String>> list = machineryTypeService.tree(machineryType);

        return AjaxResult.success(machineryTypes);
    }

    /**
     * 添加设备类型
     * @param machineryType
     * @return
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MachineryType machineryType){
        machineryTypeService.insert(machineryType);
        return AjaxResult.success();
    }

    /**
     * 根据父ID查询父级信息
     * @param parentId 父设备类型ID
     * @return 父设备类型信息
     */
    @GetMapping("/getParentInfo")
    public AjaxResult getParentInfo(@RequestParam("parentId") Integer parentId){
        MachineryType parentMachineryType = machineryTypeService.selectParent(parentId);
        return AjaxResult.success(parentMachineryType);
    }

    /**
     * 更新设备类型
     * @param machineryType
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody MachineryType machineryType){
        machineryTypeService.update(machineryType);
        return AjaxResult.success();
    }

    @GetMapping("/delete")
    public AjaxResult delete(@RequestParam("machineryTypeId") Integer machineryTypeId){
        machineryTypeService.delete(machineryTypeId);
        return AjaxResult.success();
    }
}
